﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace CoolCode.Controls
{
    public class Window
    {
        private static Loading loading;
        private readonly  static string loadingName = "loading";

        private static Panel dialogGrid;
        private readonly static string dialogGridName = "dialogGrid";

        private readonly  static string commonDialog = "commonDialog";
        static Window()
        {
            if (loading == null)
            {
                UserControl rootPage = Application.Current.RootVisual as UserControl;
                if (rootPage != null)
                {
                    loading = rootPage.FindName(loadingName) as Loading;
                }
            }

            if (dialogGrid == null)
            {
                UserControl rootPage = Application.Current.RootVisual as UserControl;
                if (rootPage != null)
                {
                    dialogGrid = rootPage.FindName(dialogGridName) as Panel;
                }
            }
        }

        public static void ShowLoading()
        {
            ShowLoading("");
        }

        public static void ShowLoading(string message)
        {
            if (loading != null)
            {
                loading.Dispatcher.BeginInvoke(() =>
                {
                    loading.Visibility = Visibility.Visible;
                    loading.SetLoadingText(message);
                });
            }
        }

        public static void HideLoading()
        {
            if (loading != null)
                loading.Dispatcher.BeginInvoke(() => loading.Visibility = Visibility.Collapsed);
        }

        public static void Alert(string msg)
        {
            if (dialogGrid != null)
                dialogGrid.Dispatcher.BeginInvoke(() =>
                {
                    dialogGrid.Children.Clear();
                    dialogGrid.Children.Add(new Alert(msg));
                }
                    );
        }

        public static void ShowDialog(Panel panel, ModalDialog dialog)
        {  
            Border placeHolder =panel.FindName(commonDialog ) as Border;
            if (placeHolder == null)
            {
                placeHolder = new Border
                {
                    Name = commonDialog ,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    Background = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100)),
                    Opacity = 0.7
                };
              panel.Children.Add(placeHolder);
            }
            placeHolder.Child = dialog;
            placeHolder.Visibility = Visibility.Visible; 

        }

        public static void CloseDialog()
        {
            UserControl rootPage = Application.Current.RootVisual as UserControl;
            Border placeHolder =rootPage.FindName(commonDialog ) as Border;
            if (placeHolder != null)
            {
                placeHolder.Child = null;
                placeHolder.Visibility = Visibility.Collapsed ;
            }
        }

        //public static void Confirm(string msg, OnConfirm onConfirm)
        //{
        //    if (dialogGrid != null)
        //        dialogGrid.Dispatcher.BeginInvoke(() =>
        //        {
        //            dialogGrid.Children.Clear();
        //            dialogGrid.Children.Add(new Confirm(msg, onConfirm));
        //        }
        //        );
        //}

        //public static void ShowOpPanel(string title, UserControl panel, OnSubmit onSubmit)
        //{
        //    if (dialogGrid != null)
        //        dialogGrid.Dispatcher.BeginInvoke(() =>
        //        {
        //            dialogGrid.Children.Clear();
        //            dialogGrid.Children.Add(new OPPanel(title, panel, onSubmit));
        //        }
        //        );
        //}

        //private static Panel dialogGrid;
        //private static string dialogGridName = "dialogGrid";

        //private static Panel loading;
        //private static string loadingName = "loading";

        //static Window()
        //{
        //    if (dialogGrid == null)
        //    {
        //        UserControl rootPage = Application.Current.RootVisual as UserControl;
        //        if (rootPage != null)
        //        {
        //            dialogGrid = rootPage.FindName(dialogGridName) as Panel;
        //        }
        //    }

        //    if (loading == null)
        //    {
        //        UserControl rootPage = Application.Current.RootVisual as UserControl;
        //        if (rootPage != null)
        //        {
        //            loading = rootPage.FindName(loadingName) as Panel;
        //        }
        //    }
        //}

        //public static void Alert(string msg)
        //{
        //    if (dialogGrid != null)
        //    {
        //        dialogGrid.Dispatcher.BeginInvoke(() =>
        //        {
        //            dialogGrid.Children.Clear();
        //            dialogGrid.Children.Add(new Alert(msg));
        //        });
        //        //dialogGrid.Children.Clear();
        //        //dialogGrid.Children.Add(new Alert(msg));
        //    }
        //}
        ////public static void Alert(string msg)
        ////{
        ////    if (dialogGrid != null)
        ////    {
        ////        DialogPanel dialog = new DialogPanel();
        ////        dialog.Content = new TextBlock { Text = msg };
        ////        dialog.DragEnable = false;
        ////        dialogGrid.Dispatcher.BeginInvoke(() =>
        ////        {
        ////            dialogGrid.Children.Clear();
        ////            dialogGrid.Children.Add(dialog);
        ////        });
        ////    }
        ////}

        ////public static void Alert(string msg)
        ////{
        ////    DialogPanel dialog = new DialogPanel();
        ////    dialog.Content =new TextBlock{ Text=msg};
        ////    dialog.DragEnable = false ;
        ////    Popup p = new Popup();
        ////    p.Child = dialog;      
        ////    // Set where the popup will show up on the screen.
        ////    p.VerticalOffset = 25;
        ////    p.HorizontalOffset = 25;
        ////    p.Opacity = 0.5;
        ////    // Open the popup.
        ////    p.IsOpen = true;

        ////}

        //public static void ShowLoading()
        //{
        //    ShowLoading("");
        //}

        //public static void ShowLoading(string message)
        //{
        //    if (loading != null)
        //    {
        //        loading.Dispatcher.BeginInvoke(() =>
        //        {
        //            if (loading.Children.Count == 0)
        //            {
        //                Loading load = new Loading();
        //                load.Visibility = Visibility.Visible;
        //                load.SetLoadingText(message);
        //                loading.Children.Clear();
        //                loading.Children.Add(load);
        //            }
        //            else
        //            {
        //                Loading load = loading.Children[0] as Loading;
        //                if (load != null)
        //                {
        //                    load.SetLoadingText(message);
        //                    load.Visibility = Visibility.Visible;
        //                }
        //            }
        //        });
        //    }
        //}

        //public static void HideLoading()
        //{
        //    if (loading != null)
        //        loading.Dispatcher.BeginInvoke(() => { if (loading.Children.Count == 0)return; loading.Children[0].Visibility = Visibility.Collapsed; });
        //}
    }
}